﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Entities;
using System.Data.SqlClient;

namespace DAL
{
    public class DistanceDAL : BaseObject
    {
        #region Constructor

        ///<summary>
        ///Initializes a new instance of the <see cref="UserDal"/> 
        ///class with the specified <see cref="BaseDAL"/>.
        ///</summary>
        public DistanceDAL()
            : base()
        {

        }

        #endregion

        #region Public Methods
        ///<summary>
        ///Create Obj from ireader.
        ///</summary>

        private Distance GetObjectFromReader(IDataReader reader)
        {
            Distance temp = new Distance();
            temp.StationA = (reader["StationA"] is DBNull) ? int.MinValue : (System.Int32)reader["StationA"];
            temp.StationB = (reader["StationB"] is DBNull) ? int.MinValue : (System.Int32)reader["StationB"];
            temp.Distances = (reader["Distance"] is DBNull) ? int.MinValue : (System.Int32)reader["Distance"];
            return temp;
        }

        ///<summary>
        ///Get List Distance and store in a list 
        ///</summary>
        public List<Distance> GetList_Distance(string whereSql, string orderBy)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@WhereCondition", whereSql));
                cmd.Parameters.Add(new SqlParameter("@OrderByExpression", orderBy));
                conn.Open();
                List<Distance> lst = new List<Distance>();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        lst.Add(GetObjectFromReader(reader));
                    }
                }
                conn.Close();
                return lst;
            }
        }

        ///<summary>
        ///Get All Distance and store in a list 
        ///</summary>
        public List<Distance> GetAll_Distance()
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                List<Distance> lst = new List<Distance>();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (reader.Read())
                    {
                        lst.Add(GetObjectFromReader(reader));
                    }
                }
                conn.Close();
                return lst;
            }
        }

        ///<summary>
        ///Get List Distance by ID
        ///</summary>
        public Distance Get_Distance_By_ID(int StationA, int StationB)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand(usp_SelectDistancesByID, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@StationA", StationA));
                cmd.Parameters.Add(new SqlParameter("@StationB", StationB));
                Distance temp = new Distance();
                conn.Open();
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                    {
                        temp = GetObjectFromReader(reader);
                    }
                }
                conn.Close();
                return temp;
            }
        }

        ///<summary>
        ///Using for paging data 
        ///</summary>
        /// <param name="pageIndex">Number of pages</param>
        /// <param name="PageSize">Number of a rows per page</param>
        /// <param name="conditions">Where  fields</param>
        /// <param name="groupBy">Group by fields</param>
        public DataTable Get_DistanceAsPaging(int pageIndex, int pageSize, string sSortColumn, string sFields, string sWhere, string sGroupBy)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                cmd.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                cmd.Parameters.Add(new SqlParameter("@SortColumn", sSortColumn));
                cmd.Parameters.Add(new SqlParameter("@Fields", sFields));
                cmd.Parameters.Add(new SqlParameter("@Where", sWhere));
                cmd.Parameters.Add(new SqlParameter("@Group", sGroupBy));
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                try
                {
                    conn.Open();
                    DataTable tbl = new DataTable();
                    dataAdapter.Fill(tbl);
                    conn.Close();
                    return tbl;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Insert new Distance
        ///</summary>
        public void Insert_Distance(Distance temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@StationA", temp.StationA));
                cmd.Parameters.Add(new SqlParameter("@StationB", temp.StationB));
                cmd.Parameters.Add(new SqlParameter("@Distances", temp.Distances));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Update a Distance
        ///</summary>
        public void Update_Distance(Distance temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@StationA", temp.StationA));
                cmd.Parameters.Add(new SqlParameter("@StationB", temp.StationB));
                cmd.Parameters.Add(new SqlParameter("@Distances", temp.Distances));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        public void Delete_Distance(Distance temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@StationA", temp.StationA));
                cmd.Parameters.Add(new SqlParameter("@StationB", temp.StationB));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        #endregion
    }
}
